#pragma once
#include "TextBox.h"
#include <vector>
#include "KeyLock.h"
#include <map>
#include <string>

namespace game
{
	class Menu
	{
		std::map<int, std::string> _indexNames;
		sf::RenderWindow * _renderWindow;
		game::KeyLock * _input;
		std::vector<TextBox*> _textBoxes;
		TextBox* _slider;
		int _sliderPos;
		int _sliderMaxPos;
	public:

		void offsetSlider(int offset)
		{
			_sliderPos+=offset;
			if (_sliderPos > _sliderMaxPos){_sliderPos = 0;}
			if (_sliderPos < 0){_sliderPos = _sliderMaxPos;}
			sf::Vector2u pos = _textBoxes.at(_sliderPos)->getPos();
			
			pos.x += _textBoxes.at(_sliderPos)->getSize().x;
			_slider->setPos(pos);
		}

		bool isSelected() const
		{
			return _input->isKeyPressed(sf::Keyboard::Return);
		}

		int getSliderPos()const{return _sliderPos;}

		std::string getChoice()
		{
			return _indexNames[_sliderPos];
		}
		~Menu()
		{
			_indexNames.clear();
			_textBoxes.clear();
			delete _slider;

		}
		Menu(std::vector<TextBox*> textBoxes, game::KeyLock * input, sf::RenderWindow * rw)
		{
			_renderWindow = rw;
			_input = input;
			_sliderPos = 0;
			_textBoxes = textBoxes;
			for (int i  =0; i < _textBoxes.size(); i++)
			{
				_indexNames[i] = _textBoxes[i]->getName();
			}
			_slider = new TextBox("[o]","",16,_textBoxes.at(0)->getPos(),sf::Vector2u(16,16));
			_sliderMaxPos  = _textBoxes.size()-1;	//index of last valid textBox;
			offsetSlider(0);
		}
		void Update()
		{
			bool up = _input->isKeyPressed(sf::Keyboard::Up);
			bool down =_input->isKeyPressed(sf::Keyboard::Down);
			//bool enter = _input->isKeyPressed(sf::Keyboard::Return);

			//if (enter){std::cout<< " returned" <<std::endl;}

			if (up){offsetSlider(-1);}
			if (down){offsetSlider(+1);}
		}
		
		void Draw() const
		{
			_slider->draw(*_renderWindow);
			for each(TextBox* b in _textBoxes)
			{
				b->draw(*_renderWindow);
			}
		}
	};
}